package com.hyl.boottest.controller;

import com.alibaba.fastjson.JSON;
import com.hyl.boottest.entity.ReturnPerson;
import com.hyl.boottest.service.ReturnPersonService;
import com.hyl.boottest.util.PageUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2023/3/16 9:02
 **/
@RestController
public class OptionController {

	@Resource
	private ReturnPersonService service;

	@RequestMapping("/list")
	public PageUtil list(@RequestParam(required = false) String returnDate,
	                   @RequestParam(required = false,defaultValue = "1")  Integer currentPage,
	                   @RequestParam(required = false,defaultValue = "3") Integer showSize){

		//封装==》响应给前台
		PageUtil<ReturnPerson> pageUtil = service.listPage(returnDate, currentPage, showSize);
		return pageUtil;
	}

	@RequestMapping("/info")
	public ReturnPerson info(Integer id){
		ReturnPerson person = service.selectById(id);
		return person;
	}

	@RequestMapping("/add")
	public String add(ReturnPerson person){
		int ret = service.insert(person);
		Map<String,Object> params = new HashMap<>();
		if(ret>0) {
			params.put("code", "1000");
			params.put("msg", "成功");
		}else{
			params.put("code", "1001");
			params.put("msg", "失败");
		}
		return JSON.toJSONString(params);
	}

	@RequestMapping("/modify")
	public String modify(ReturnPerson person){
		int ret = service.updateById(person);
		Map<String,Object> params = new HashMap<>();
		if(ret>0) {
			params.put("code", "1000");
			params.put("msg", "成功");
		}else{
			params.put("code", "1001");
			params.put("msg", "失败");
		}
		return JSON.toJSONString(params);
	}
}


